/**
 * Created by lixiaodi on 2018/11/5.
 */
mini.parse();
mini_debugger = false;
var tree = mini.get("tree1");       //得到树
var grid = mini.get("datagrid1");   //得到表
//树结构显示
$(function(){
    $.ajax({
        url: ajaxUrl + "/TreeController/getAllTree.do",
        type:"post",
        success: function (data) {
            tree.setData(data.data);
        },
        error:function(data){
            mini.alert("失败");
        }
    });
})

//选择节点
tree.on("nodeselect", function (e) {
    var nodes = e.node;
    if (nodes) {
        var depId = nodes.pk;
        var types = nodes.id.substring(0,2);
        $.ajax({
            url:ajaxUrl + "/personnerlUser/getPersonnerListBypid.do",
            type:"POST",
            data : {
                id : depId,
                types : types
            },
            success : function(data){
            	console.log(data);
                if(data.data){
                	var personData = data.data;
                	for(var i = 0; i < personData.length; i++){
                		//日期格式化
                		if(personData[i].createDate){
                			personData[i].createDate = formatDate(personData[i].createDate);
                		}else{
                			personData[i].createDate = '';
                		}
                		//出生日期
                		if(personData[i].dateBirth){
                			personData[i].dateBirth = formatDate(personData[i].dateBirth);
                		}else{
                			personData[i].dateBirth = '';
                		}
                		//是否操作员
                		personData[i].isOperator = personData[i].isOperator == 1 ? '是' : '否';
                		//模块状态
                		if(personData[i].modularState){
                			personData[i].modularState = personData[i].modularState == 1 ? '开启' : '关闭';
                		}else{
                			personData[i].modularState = '';
                		}
                	}
                    grid.setData(personData);
                }else{
                    showTips(data.msg);
                    grid.setData(data.data);
                }
            },
            error:function(data){
                mini.alert("失败");
            }
        });
    } else {
        grid.setData([]);
        grid.setTotalCount(0);
    }
});
var url = bootPATH + "../frame/frame1/pages/";
//增加
function add() {
    var selectNode = tree.getSelectedNode();
    var parNodeObj = {};
    if(selectNode.id.substring(0,2) == "de"){
        var parNode = tree.getAncestors(selectNode);
        //得到集团id和公司id
        parNodeObj.depId = selectNode.pk;
        for(var i = 0; i < parNode.length; i++){
            if(parNode[i].id.substring(0,2) == "gr"){
                parNodeObj.groupId = parNode[i].pk;
            }else if(parNode[i].id.substring(0,2) == "co"){
                parNodeObj.companyId = parNode[i].pk;
            }
        }
    }else if(selectNode.id.substring(0,2) == "co"){
        parNodeObj.groupId = selectNode.pid.replace(/[^0-9]/ig,"");
        parNodeObj.companyId = selectNode.pk;
        parNodeObj.depId = "";
    }else if(selectNode.id.substring(0,2) == "gr"){
        parNodeObj.groupId = selectNode.pk;
        parNodeObj.companyId = "";
        parNodeObj.depId = "";
    }
    mini.open({
        url: url + "alertBox/EmployeeWindow.html",
        title: "新增员工", width: 600, height: 600,
        onload: function () {
            var iframe = this.getIFrameEl();
            var data = {
                action: "new",
                parsId : parNodeObj
            };
            iframe.contentWindow.SetData(data);
        },
        ondestroy: function (action) {
            //通过action的值判断子页面的关闭方式
        	refresh();
        }
    });
}
//编辑
function edit() {
    //得到被选中的行数据
    var row = grid.getSelected();
    if (row) {
        mini.open({
            url: url + "alertBox/EmployeeWindow.html",
            title: "编辑员工", width: 600, height: 600,
            onload: function () {
                var iframe = this.getIFrameEl();
                //通过action判断是编辑还是增加
                var data = { action: "edit", id: row.perId };
                iframe.contentWindow.SetData(data);
            },
            ondestroy: function (action) {
            	refresh();
            }
        });

    } else {
        mini.alert("请选中一条记录");
    }

}
//删除
function remove() {
    var rows = grid.getSelecteds();
    if (rows.length > 0) {
        if (mini.confirm("确定删除选中记录？")) {
            var ids = [];
            for (var i = 0, l = rows.length; i < l; i++) {
                var r = rows[i];
                ids.push(r.userId);
            }
            var id = ids.join(',');
            grid.loading("操作中，请稍后......");
            $.ajax({
                url: ajaxUrl + "/user/deleteUserInfo.do?userIds=" + id,
                type:"post",
                success: function (text) {
                	refresh();
                },
                error: function () {
                }
            });
        }
    } else {
        mini.alert("请选中一条记录");
    }
}
//刷新
function refresh(){
	var selectNode = tree.getSelectedNode();
	var nodes = selectNode;
	var depId = nodes.pk;
    var types = nodes.id.substring(0,2);
    $.ajax({
        url:ajaxUrl + "/personnerlUser/getPersonnerListBypid.do",
        type:"POST",
        data : {
            id : depId,
            types : types
        },
        success : function(data){
            if(data.data){
            	var personData = data.data;
            	for(var i = 0; i < personData.length; i++){
            		//日期格式化
            		if(personData[i].createDate){
            			personData[i].createDate = formatDate(personData[i].createDate);
            		}else{
            			personData[i].createDate = '';
            		}
            		//出生日期
            		if(personData[i].dateBirth){
            			personData[i].dateBirth = formatDate(personData[i].dateBirth);
            		}else{
            			personData[i].dateBirth = '';
            		}
            		//是否操作员
            		personData[i].isOperator = personData[i].isOperator == 1 ? '是' : '否';
            		//模块状态
            		if(personData[i].modularState){
            			personData[i].modularState = personData[i].modularState == 1 ? '开启' : '关闭';
            		}else{
            			personData[i].modularState = '';
            		}
            	}
                grid.setData(personData);
            }else{
                showTips(data.msg);
                grid.setData(data.data);
            }
        },
        error:function(data){
            mini.alert("失败");
        }
    });
}